//Documentation complete
/**
 * @author Jason
 * @version 2009MR20
 */

package ui.internal.ajax;

import problemDomain.*;
import control.*;
import static problemDomain.ErrorBean.*;
import static utilities.XMLTools.*;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.*;

/**
 * Servlet implementation class MaintainReservation
 */
public class MaintainReservation extends HttpServlet {
	
	/* Constants */
	
	private static final long serialVersionUID = 4992479034840765876L;
	
	/* END Constants */
	
	
	/* (HttpServlet) Methods */
	
	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		HttpSession session = request.getSession();
		response.setContentType("text/xml");
		if (session != null) {
			String action = request.getParameter("a");
			if (action != null) {
				if (action.equalsIgnoreCase("c"))
					create(request, response);
				else if (action.equalsIgnoreCase("r"))
					report(request, response);
				else if (action.equalsIgnoreCase("rd"))
					reportByDate(request, response);
				else if (action.equalsIgnoreCase("u"))
					update(request, response);
				else if (action.equalsIgnoreCase("d"))
					delete(request, response);
				else if (action.equalsIgnoreCase("l"))
					listAll(response);
				else if (action.equalsIgnoreCase("o"))
					listOperationHour(response);
			} else
				response.setStatus(HttpServletResponse.SC_NO_CONTENT);
		} else
			response.setStatus(HttpServletResponse.SC_NO_CONTENT);
	}
	
	/* END (HttpServlet) Methods */
	
	
	/* Local Methods */
	
	private void create (HttpServletRequest request, HttpServletResponse response) throws IOException {
		HttpSession session = request.getSession(true);
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String numPeople = request.getParameter("numPeople");
		String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		String hour = request.getParameter("hour");
		String minute = request.getParameter("minute");
		String description = request.getParameter("description");
		EmployeeAccountBean employee = (EmployeeAccountBean) session.getAttribute("employee");
		PrintWriter out = response.getWriter();
		try {
			ReservationControl resCtrl = new ReservationControl();
			resCtrl.create(firstname, lastname, phone, numPeople, day, month, year, hour, minute, description, employee);
			out.write(ErrorBean.getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void report (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		
		PrintWriter out = response.getWriter();
		try {
			ReservationControl resCtrl = new ReservationControl();
			List<ReservationBean> reportList = resCtrl.search(firstname, lastname, phone, day, month, year);
			out.write(encapsulateBeanListXML(reportList, "reservations"));
		}
		catch (ErrorBean e) {
			out.println(e.toXML());
		}
	}
	
	private void reportByDate (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		String type = request.getParameter("type");
		
		PrintWriter out = response.getWriter();
		try {
			ReservationControl resCtrl = new ReservationControl();
			List<ReservationBean> reportList = resCtrl.searchByDate(day, month, year, type);
			out.write(encapsulateBeanListXML(reportList, "reservations"));
		}
		catch (ErrorBean e) {
			out.println(e.toXML());
		}
	}
	
	private void update (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		String firstname = request.getParameter("firstname");
		String lastname = request.getParameter("lastname");
		String phone = request.getParameter("phone");
		String numPeople = request.getParameter("numPeople");
		String day = request.getParameter("day");
		String month = request.getParameter("month");
		String year = request.getParameter("year");
		String hour = request.getParameter("hour");
		String minute = request.getParameter("minute");
		String description = request.getParameter("description");
		PrintWriter out = response.getWriter();
		try {
			ReservationControl resCtrl = new ReservationControl();
			resCtrl.update(id, firstname, lastname, phone, numPeople, day, month, year, hour, minute, description);
			out.write(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void delete (HttpServletRequest request, HttpServletResponse response) throws IOException {
		String id = request.getParameter("id");
		PrintWriter out = response.getWriter();
		try {
			ReservationControl resCtrl = new ReservationControl();
			resCtrl.delete(id);
			out.println(getSuccess().toXML());
		} catch (ErrorBean e) {
			out.println(e.toXML());
		}
		
	}
	
	private void listAll (HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			ReservationControl resCtrl = new ReservationControl();
			List<ReservationBean> list = resCtrl.listAll();
			out.write(encapsulateBeanListXML(list, "reservations"));
			//for (int i = 0; i < list.size(); i++)
				//System.out.println(list.get(i));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	private void listOperationHour(HttpServletResponse response) throws IOException {
		PrintWriter out = response.getWriter();
		try {
			ReservationControl resCtrl = new ReservationControl();
			List<String> list = resCtrl.listOperationHour();
			out.write(encapsulateStringListXML(list));
		} catch (ErrorBean e) {
			out.write(e.toXML());
		} finally {
			out.close();
		}
	}
	
	/* END Local Methods */
	
}
